package com.manong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.domain.Warehouse;
import com.manong.exception.BusinessExp;
import com.manong.mapper.WarehouseMapper;
import com.manong.query.WarehouseQueryObject;
import com.manong.service.WarehouseService;
import com.manong.vo.GoodsWarehouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    WarehouseMapper warehouseMapper;

    @Override
    public PageInfo query(WarehouseQueryObject queryObject) {
        if(queryObject.getPageSize() == -1){
            List<Warehouse> warehouses = warehouseMapper.selectForList(queryObject);
            return new PageInfo(warehouses);
        } else {
            //PageHelper进行分页操作
            //告诉PageHelper 当前页是多少 每页显示数据
            PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
            //调用分页方法
            List<Warehouse> warehouses = warehouseMapper.selectForList(queryObject);
            return new PageInfo(warehouses);
        }
    }

    @Override
    public void delete(Integer id) {
        if(id == null){
            throw new BusinessExp("参数不能为空");
        }
        int count = warehouseMapper.deleteByPrimaryKey(id);
        // count 影响行数， 如果count == 0 表示删除失败
        if(count == 0){
            throw new BusinessExp("删除失败");
        }
    }

    @Override
    public void saveOrUpdate(Warehouse warehouse) {
        if(warehouse == null){
            throw new BusinessExp("非法操作");
        }
        if(warehouse.getId() == null){
            this.save(warehouse);
        } else {
            this.update(warehouse);
        }
    }


    @Transactional
    @Override
    public void save(Warehouse warehouse) {
        // 拿到role对象
        Warehouse warehouseInfo = warehouse;
        // 把role存在数据库库中
        warehouseMapper.insert(warehouseInfo);
    }


    @Override
    public void update(Warehouse warehouse) {
        // vo对象中获取role对象
        Warehouse warehouseInfo = warehouse;
        warehouseMapper.updateByPrimaryKey(warehouseInfo);
    }

    @Override
    public Warehouse get(Integer id) {
        if(id == null){
            throw new BusinessExp("id不能为空");
        }
        Warehouse warehouse = warehouseMapper.selectWarehouseWithGoods(id);
        if(warehouse == null){
            throw new BusinessExp("非法操作");
        }
        return warehouse;
    }

    @Override
    public int getTotalWarehouseCount() {
        return warehouseMapper.selectAll().size();
    }

    @Override
    public List<Warehouse> getWarehousesByManager(Integer manager) {
        return warehouseMapper.selectByManager(manager);
    }

    @Override
    public List<GoodsWarehouseVo> getGoodsInWarehouse(Integer warehouseId) {
        return warehouseMapper.getGoodsInWarehouse(warehouseId);
    }
}
